package demo.ssm.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import demo.ssm.dao.mapper.RoleMapper;
import demo.ssm.dao.mapper.RoleMapperCustom;
import demo.ssm.pojo.po.Department;
import demo.ssm.pojo.po.DepartmentExample;
import demo.ssm.pojo.po.Role;
import demo.ssm.pojo.po.RoleExample;
import demo.ssm.pojo.vo.RoleCustom;
import demo.ssm.pojo.vo.RoleQueryVo;
import demo.ssm.process.context.Config;
import demo.ssm.process.result.ResultUtil;
import demo.ssm.service.RoleService;
import demo.ssm.util.MyUtil;
import demo.ssm.util.UUIDBuild;
import demo.ssm.util.easyuitree;

public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleMapperCustom roleMapperCustom;
	@Autowired
	private RoleMapper roleMapper;
	
	//查询上级职位
	private Role findRoleByPname(String pname) throws Exception{
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andRnameEqualTo(pname);
		List<Role> list = roleMapper.selectByExample(roleExample);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
		}
	//根据职位名称查询职位
	private Role findRoleByRname(String rname) throws Exception{
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andRnameEqualTo(rname);
		List<Role> list = roleMapper.selectByExample(roleExample);
		if (list != null && list.size() != 0) {
			return list.get(0);
		}
		return null;
		}
	@Override
	public int findRoleCount() throws Exception {

		return roleMapperCustom.findRoleCount();
	}

	@Override
	public List<RoleCustom> findRoleList(RoleQueryVo roleQueryVo)
			throws Exception {
		return roleMapperCustom.findRoleList(roleQueryVo);
		
	}
	
	@Override
	//添加职位
	public void insertRole(RoleCustom roleCustom) throws Exception {
		// TODO Auto-generated method stub
	
		//职位名称不能为空
		if(roleCustom.getRname()==null || roleCustom.getRname()==""){
			ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 224,
					null));
		}
		Role rolername = new Role();
		rolername = this.findRoleByRname(roleCustom.getRname());
		if(rolername!=null){
			ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 225,
					null));
		}
		Role role = new Role();
		if(roleCustom.getPname()==null || roleCustom.getPname()==""){
			role.setLevel(1);		
		}else{
			Role rolep = new Role();
			rolep = this.findRoleByPname(roleCustom.getPname());
			role.setLevel(rolep.getLevel()+1);
		}				
		role.setRname(roleCustom.getRname());
		role.setPname(roleCustom.getPname());
		role.setRoleid(UUIDBuild.getUUID());
		roleMapper.insert(role);
	}

	@Override
	public void deleteRole(String id) throws Exception {
		// TODO Auto-generated method stub
		Role role = this.selectRoleById(id);
		if(role==null){
			ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 226,
					null));
		}
		roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Role selectRoleById(String id) throws Exception {
		// TODO Auto-generated method stub
		
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateRole(RoleCustom roleCustom, String id) throws Exception {
		// TODO Auto-generated method stub
		Role findrole = this.selectRoleById(id);//要修改的职位是否存在
		if(findrole==null){
			ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 226,
					null));
		}
		if(!findrole.getRname().equals(roleCustom.getRname())){
			ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 225,
					null));
		}
		findrole.setRname(roleCustom.getRname());
		findrole.setPname(roleCustom.getPname());
		roleMapper.updateByPrimaryKey(findrole);
	}
	@Override
	public List<RoleCustom> findRoleTree() throws Exception {
		// TODO Auto-generated method stub
		List<RoleCustom> roleList = roleMapperCustom.findRoleTree();
		return roleList;
	}
	
//	private void roleTreeList(Collection<RoleCustom> topList,List<RoleCustom> list){
//		for(RoleCustom top:topList){
//			RoleCustom roleCustom = new RoleCustom();
//			roleCustom.setId(top.getLevel());
//			roleCustom.setText(top.getRname());
//			list.add(roleCustom);
//			roleTreeList(top.getChildren(),list);
//		}
//	}
	
}
